package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.User;
import com.zyd.shiro.business.service.CourseService;
import com.zyd.shiro.business.vo.CourseConditionVO;
import com.zyd.shiro.persistence.beans.ChooseCourse;
import com.zyd.shiro.persistence.beans.Course;
import com.zyd.shiro.persistence.beans.Major;
import com.zyd.shiro.persistence.mapper.CourseMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageInfo<Course> findPageBreakByCondition(CourseConditionVO vo, User user) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        if("TEACHER".equals(user.getUserType())){
            vo.setUserType("TEACHER");
            vo.setTeacherId(user.getId());
        }
        List<Course> courses = courseMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(courses)) {
            return null;
        }
        if("STUDENT".equals(user.getUserType())){
            Map<Long, ChooseCourse> chooseMap = courseMapper.selectCourseChoose(courses,user.getId());
            for(Course course:courses){
                course.setIsSelect(ObjectUtils.isEmpty(chooseMap.get(course.getId()))?0:1);
            }
        }
        PageInfo bean = new PageInfo<Course>(courses);
        bean.setList(courses);
        return bean;
    }

    @Override
    public boolean removeCourse(Long courseId, Long userId) {
        courseMapper.deleteChooseCourse(courseId,userId);
        return courseMapper.deleteByPrimaryKey(courseId) > 0;
    }

    @Override
    public int chooseCourse(Long courseId, Long userId) {
        return courseMapper.insertChooseCourse(courseId,userId);
    }

    @Override
    public int chooseCancel(Long courseId, Long userId) {
        return courseMapper.deleteChooseCourse(courseId,userId);
    }

    @Override
    public Course insert(Course entity) {
        Assert.notNull(entity, "Course不可为空！");
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        courseMapper.insert(entity);
        return entity;
    }

    @Override
    public void insertList(List<Course> entities) {

    }

    @Override
    @Transactional
    public boolean removeByPrimaryKey(Long primaryKey) {
       return false;
    }

    @Override
    public boolean update(Course entity) {
        return false;
    }

    @Override
    public boolean updateSelective(Course entity) {
        Assert.notNull(entity, "Course不可为空！");
        entity.setUpdateTime(new Date());
        return courseMapper.updateByPrimaryKeySelective(entity) > 0;
    }

    @Override
    public Course getByPrimaryKey(Long primaryKey) {
        Assert.notNull(primaryKey, "PrimaryKey不可为空！");
        Course course = courseMapper.selectByPrimaryKey(primaryKey);
        return null == course ? null : course;
    }

    @Override
    public Course getOneByEntity(Course entity) {
        return null;
    }

    @Override
    public List<Course> listAll() {
        return null;
    }

    @Override
    public List<Course> listByEntity(Course entity) {
        return null;
    }

}
